<?php
namespace app\app\model;
use think\Model;
use think\Db;

class VideoModel extends Model{

    public $user_id;
    protected $table = "a_video";

    /**
     * 获取video列表
     */
    public function getList(){
        return $this->field('id,title as name,cover_img as src,url as vid,click_num as num,Playback')->where('status','eq',1)->select();
    }

    /**
     * 获取单个视频
     */
    public function getOne($id){
        $video = $this->field('id,title as name,cover_img as src,url as vid,click_num as num,Playback')->where('id','eq',$id)->find();
        $message = Db::name('message')->alias('a')->field('a.id,a.content,FROM_UNIXTIME(a.create_time,"%Y-%m-%d %H:%i") as create_time,b.nickname,b.avatar_url')
            ->join('users b','a.user_id=b.id','left')->where('a.video_id','eq',$id)->select();
        $video['message'] = $message;
        $video['message_num'] = count($message);
        //浏览数+1
        $this->query('update a_video set Playback = Playback + 1 where id ='.$id);
        return $video;
    }


    /**
     * @param $id
     * 点赞video
     */
    public function click($id){
        $click = Db::name('video_click')->where('user_id','eq',$this->user_id)->where('video_id','eq',$id)->find();
        if( !$click ){
            Db::name('video_click')->insert(array('video_id'=>$id,'user_id'=>$this->user_id,'create_time'=>time()));
            Db::query('update a_video set click_num = click_num + 1 where id='.$id);
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $data
     * 留言
     */
    public function leaveMessage($data){
        return Db::name('message')->insert(array('user_id'=>$this->user_id,'content'=>$data['content'],'video_id'=>$data['id'],'create_time'=>time()));

    }
}